# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>

# NOTE: This is a fork of LLVM project needed for emscripten. It provides
#   JSBackend and WebAssembly, these will be hopefully someday merged into
#   upstream LLVM.
# TODO: Remove unneeded binaries.
pkgname=emscripten-fastcomp
# Keep in sync with package emscripten!
pkgver=1.39.0
pkgrel=1
pkgdesc="The Clang + LLVM backend for Emscripten"
url="https://emscripten.org/"
arch="all"
license="UOI-NCSA"
depends="python3"
makedepends="cmake paxmark zlib-dev"
source="emscripten-fastcomp-$pkgver.tar.gz::https://github.com/emscripten-core/emscripten-fastcomp/archive/$pkgver.tar.gz
	emscripten-fastcomp-clang-$pkgver.tar.gz::https://github.com/emscripten-core/emscripten-fastcomp-clang/archive/$pkgver.tar.gz
	llvm-fix-build-with-musl-libc.patch
	none-build-type.patch
	"
options="!check"  # some tests are broken, don't bother with it for now

_prefix="usr/lib/$pkgname"
ldpath="/$_prefix/lib"
# Avoid conflicts in provided so:* names with llvm package.
sonameprefix="$pkgname:"

prepare() {
	ln -sf ../../$pkgname-clang-$pkgver "$builddir"/tools/clang

	default_prepare
}

build() {
	mkdir -p "$builddir"/build
	cd "$builddir"/build

	# XXX: Is it a good idea to omit X86 in LLVM_TARGETS_TO_BUILD?
	# https://github.com/kripken/emscripten-fastcomp/issues/174
	cmake .. \
		-DCMAKE_BUILD_TYPE=None \
		-DCMAKE_INSTALL_PREFIX="/$_prefix" \
		-DCMAKE_VERBOSE_MAKEFILE=NO \
		-DPYTHON_EXECUTABLE=/usr/bin/python3 \
		-DCLANG_BUILD_EXAMPLES=OFF \
		-DCLANG_INCLUDE_TESTS=OFF \
		-DLLVM_BUILD_DOCS=OFF \
		-DLLVM_BUILD_LLVM_DYLIB=ON \
		-DLLVM_BUILD_RUNTIME=OFF \
		-DLLVM_ENABLE_ZLIB=ON \
		-DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD=WebAssembly \
		-DLLVM_HOST_TRIPLE="$CHOST" \
		-DLLVM_INCLUDE_EXAMPLES=OFF \
		-DLLVM_INCLUDE_TESTS=OFF \
		-DLLVM_LINK_LLVM_DYLIB=ON \
		-DLLVM_TARGETS_TO_BUILD=JSBackend
	make -j ${JOBS:-2}

	paxmark m \
		bin/llvm-rtdyld \
		bin/lli \
		bin/lli-child-target
}

package() {
	cd "$builddir"/build
	make install DESTDIR="$pkgdir"

	cd "$pkgdir"/$_prefix

	# emcc -v expects this file to exist.
	cp "$builddir"/emscripten-version.txt .

	# Remove unneeded files and clutter.
	rm -r include libexec share lib/clang lib/cmake
	rm lib/*.a bin/*-test
}

sha512sums="574e5b4cf86f16ae455b1f399ecec414f4d791b1b3b49afcd21e967bad62efc572e9f4cd1d2bdce050214ab25e419149bdbfc89cc868ab1019d6ca24c245db63  emscripten-fastcomp-1.39.0.tar.gz
58422eabd5795d1f9a5806862e7e2ec4489542227765064fa2f7f0930a6b96b59185302ded976189666a4fb452450fbfb37a525b8c7a3d1cc33b7e39b9909dcf  emscripten-fastcomp-clang-1.39.0.tar.gz
f84cd65d7042e89826ba6e8d48c4c302bf4980da369d7f19a55f217e51c00ca8ed178d453df3a3cee76598a7cecb94aed0775a6d24fe73266f82749913fc3e71  llvm-fix-build-with-musl-libc.patch
b0d692ae595ea72cf6950b7d152c83a301c2f24178ec800a5e9c365961c78067512d0cdedb129fa0e528217a63d861daf6b9e2105d0309f65d3936a779a79144  none-build-type.patch"
